
global _waitkey

    psect	text,class=CODE


;GTSTCK
;Address  : #00D5
;Function : Returns the joystick status
;Input    : A  - Joystick number to test (0 = cursors, 1 = port 1, 2 = port 2)
;Output   : A  - Direction
;Registers: All
;
;GTTRIG
;Address  : #00D8
;Function : Returns current trigger status
;Input    : A  - trigger button to test
;           0 = spacebar
;           1 = port 1, button A
;           2 = port 2, button A
;           3 = port 1, button B
;           4 = port 2, button B
;Output   : A  - #00 trigger button not pressed
;                #FF trigger button pressed
;Registers: AF


exttbl		equ	0xfcc1      ;main rom slot
CALSLT		equ	0x001c
GTSTCK      equ 0x00D5      ;Returns the joystick status
GTTRIG      equ 0x00D8      ;Returns current trigger status


_waitkey:
    push    ix
    push    iy    
    
    xor     a
	ld	    iy,(exttbl-1)
	ld	    ix,GTSTCK
	call	CALSLT
    push    af
    ld      a,1
	ld	    iy,(exttbl-1)
	ld	    ix,GTSTCK
	call	CALSLT
    pop     hl
    or      h
    push    af

    xor     a
	ld	    iy,(exttbl-1)
	ld	    ix,GTTRIG
	call	CALSLT
    push    af
    ld      a,1
	ld	    iy,(exttbl-1)
	ld	    ix,GTTRIG
	call	CALSLT
    pop     hl
    or      h
    
    pop     hl      ;h holds the joystick
    ld      l,a     ;l holds the trigger
    jr      nz,1f
    ld      l,h     ;return the joystick
1:
    pop     iy
    pop     ix
    ei
    ret


;    
;;----- keyboard
;; 8 RIGHT DOWN UP LEFT DEL INS HOME SPACE
;;_waitkey:
;        
;        call    joy
;        push    hl
;
;        ld      l,8
;        call    checkkbd                ; MC moves
;        
;        pop     hl
;
;        ; in l the joysitck
;        ; in a the keyboard
;
;        ld      h,0 ; 0 DOWN
;        bit     6,a
;        jr      z,_keyfound
;        bit     1,l
;        jr      z,_keyfound
;
;        inc     h ; 1 RIGHT
;        bit     7,a
;        jr      z,_keyfound
;        bit     3,l
;        jr      z,_keyfound
;
;        inc     h ; 2 UP
;        bit     5,a
;        jr      z,_keyfound
;        bit     0,l
;        jr      z,_keyfound
;
;        inc     h ; 3 LEFT
;        bit     4,a
;        jr      z,_keyfound
;        bit     2,l
;        jr      z,_keyfound
;
;        inc     h ; 4 space
;        bit     0,a
;        jr      z,_keyfound
;        bit     4,l
;        jr      z,_keyfound
;        
;        inc     h   ; no key
;
;_keyfound:
;        ld      l,h
;        ret
;        
;;----- direction table (0 = down; 1 = right; 2 = up; 3 = left, 4 = space; 5 = no move)
;
;
;
;; Bit    	Description
;;0        Input joystick pin 1      (up)
;;1        Input joystick pin 2      (down)
;;2        Input joystick pin 3      (left)
;;3        Input joystick pin 4      (right)
;;4        Input joystick pin 6      (trigger A)
;;5        Input joystick pin 7      (trigger B)
;
;joy:
;        di
;        ld a,15
;        out (0xa0),a    ; read/write from r#15
;
;        in a,(0xa2)
;        and  255-64
;        out (0xa1),a    ; set joystick in port 1
;
;        ld a,14
;        out (0xa0),a    ; read/write from r#14
;
;        in a,(0xa2)
;        ld  l,a
;        ei
;        ret
;
;;-------------------------------------
;; checkkbd: ckeck keyboard line
;; syntax:checkkbd <keyboar line #>
;; in:  l
;; out: l
;;-------------------------------------
;    
;;----- keyboard
;; 8 RIGHT DOWN UP LEFT DEL INS HOME SPACE
;
;i8255portb  equ 0a9h        ; keyboard column input
;i8255portc  equ 0aah        ; leds, motor, cassette, kbd line
;
;checkkbd:
;        in  a,(i8255portc)
;        and 011110000B          ; upper 4 bits contain info to preserve
;        or  l
;        out (i8255portc),a
;        in  a,(i8255portb)
;        ld  l,a
;        ret

